home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  35.2 KB  |  1,384 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This program is the user interface to the C compiler and possibly to
  23. other compilers.  It is used because compilation is a complicated procedure
  24. which involves running several programs and passing temporary files between
  25. them, forwarding the users switches to those programs selectively,
  26. and deleting the temporary files at the end.
  27.  
  28. CC recognizes how to compile each input file by suffixes in the file names.
  29. Once it knows which kind of compilation to perform, the procedure for
  30. compilation is specified by a string called a "spec".
  31.  
  32. Specs are strings containing lines, each of which (if not blank)
  33. is made up of a program name, and arguments separated by spaces.
  34. The program name must be exact and start from root, since no path
  35. is searched and it is unreliable to depend on the current working directory.
  36. Redirection of input or output is not supported; the subprograms must
  37. accept filenames saying what files to read and write.
  38.  
  39. In addition, the specs can contain %-sequences to substitute variable text
  40. or for conditional text.  Here is a table of all defined %-sequences.
  41. Note that spaces are not generated automatically around the results of
  42. expanding these sequences; therefore, you can concatenate them together
  43. or with constant text in a single argument.
  44.  
  45.  %%    substitute one % into the program name or argument.
  46.  %i     substitute the name of the input file being processed.
  47.  %b     substitute the basename of the input file being processed.
  48.     This is the substring up to (and not including) the last period.
  49.  %g     substitute the temporary-file-name-base.  This is a string chosen
  50.     once per compilation.  Different temporary file names are made by
  51.     concatenation of constant strings on the end, as in `%g.s'.
  52.     %g also has the same effect of %d.
  53.  %d    marks the argument containing or following the %d as a
  54.     temporary file name, so that that file will be deleted if CC exits
  55.     successfully.  Unlike %g, this contributes no text to the argument.
  56.  %w    marks the argument containing or following the %w as the
  57.     "output file" of this compilation.  This puts the argument
  58.     into the sequence of arguments that %o will substitute later.
  59.  %o    substitutes the names of all the output files, with spaces
  60.     automatically placed around them.  You should write spaces
  61.     around the %o as well or the results are undefined.
  62.     %o is for use in the specs for running the linker.
  63.     Input files whose names have no recognized suffix are not compiled
  64.     at all, but they are included among the output files, so they will
  65.     be linked.
  66.  %p    substitutes the standard macro predefinitions for the
  67.     current target machine.  Use this when running cpp.
  68.  %s     current argument is the name of a library file of some sort.
  69.         Search for that file in a standard list of directories
  70.     and substitute the full pathname found.
  71.  %a     process ASM_SPEC as a spec.
  72.         This allows config.h to specify part of the spec for running as.
  73.  %l     process LINK_SPEC as a spec.
  74.  %L     process LIB_SPEC as a spec.
  75.  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
  76.  %c    process SIGNED_CHAR_SPEC as a spec.
  77.  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
  78.  %{S}   substitutes the -S switch, if that switch was given to CC.
  79.     If that switch was not specified, this substitutes nothing.
  80.     Here S is a metasyntactic variable.
  81.  %{S*}  substitutes all the switches specified to CC whose names start
  82.     with -S.  This is used for -o, -D, -I, etc; switches that take
  83.     arguments.  CC considers `-o foo' as being one switch whose
  84.     name starts with `o'.  %{o*} would substitute this text,
  85.     including the space; thus, two arguments would be generated.
  86.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  87.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  88.  
  89. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  90. other nested % constructs or spaces, or even newlines.
  91. They are processed as usual, as described above.
  92.  
  93. Note that it is built into CC which switches take arguments and which
  94. do not.  You might think it would be useful to generalize this to
  95. allow each compiler's spec to say which switches take arguments.  But
  96. this cannot be done in a consistent fashion.  CC cannot even decide
  97. which input files have been specified without knowing which switches
  98. take arguments, and it must know which input files to compile in order
  99. to tell which compilers to run.
  100.  
  101. CC also knows implicitly that arguments starting in `-l' are to
  102. be treated as output files, and passed to the linker in their proper
  103. position among the other output files.
  104.  
  105. */
  106.  
  107. /* This defines which switches take arguments.  */
  108.  
  109. #define SWITCH_TAKES_ARG(CHAR)      \
  110.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  111.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  112.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  113.    || (CHAR) == 'L')
  114.  
  115. #include <stdio.h>
  116.  
  117. #ifdef atarist
  118. #include <types.h>
  119. #include <signal.h>
  120. #include <file.h>
  121. #include <osbind.h>
  122.  
  123. long _stksize = 8192;
  124.  
  125. #else
  126. #include <sys/types.h>
  127. #include <signal.h>
  128. #include <sys/file.h>
  129. #endif
  130.  
  131. #include "obstack.h"
  132. #include "config.h"
  133.  
  134. #ifdef USG
  135. #define R_OK 4
  136. #define W_OK 2
  137. #define X_OK 1
  138. #define vfork fork
  139. #endif
  140.  
  141. #define obstack_chunk_alloc xmalloc
  142. #define obstack_chunk_free free
  143. extern int xmalloc ();
  144. extern void free ();
  145.  
  146. /* If a stage of compilation returns an exit status >= 1,
  147.    compilation of that file ceases.  */
  148.  
  149. #define MIN_FATAL_STATUS 1
  150.  
  151. /* This is the obstack which we use to allocate many strings.  */
  152.  
  153. struct obstack obstack;
  154.  
  155. char *handle_braces ();
  156. char *save_string ();
  157. char *concat ();
  158. int do_spec ();
  159. int do_spec_1 ();
  160. int give_string ();
  161. char *find_file ();
  162.  
  163. /* config.h can define ASM_SPEC to provide extra args to the assembler
  164.    or extra switch-translations.  */
  165. #ifndef ASM_SPEC
  166. #define ASM_SPEC ""
  167. #endif
  168.  
  169. /* config.h can define CPP_SPEC to provide extra args to the assembler
  170.    or extra switch-translations.  */
  171. #ifndef CPP_SPEC
  172. #define CPP_SPEC ""
  173. #endif
  174.  
  175. /* config.h can define LINK_SPEC to provide extra args to the linker
  176.    or extra switch-translations.  */
  177. #ifndef LINK_SPEC
  178. #define LINK_SPEC ""
  179. #endif
  180.  
  181. /* config.h can define LIB_SPEC to override the default libraries.  */
  182. #ifndef LIB_SPEC
  183. #define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
  184. #endif
  185.  
  186. /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
  187. #ifndef STARTFILE_SPEC
  188. #define STARTFILE_SPEC  \
  189.   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
  190. #endif
  191.  
  192. /* This spec is used for telling cpp whether char is signed or not.  */
  193. #define SIGNED_CHAR_SPEC  \
  194.   (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}"    \
  195.    : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
  196.  
  197. /* This structure says how to run one compiler, and when to do so.  */
  198.  
  199. struct compiler
  200. {
  201.   char *suffix;            /* Use this compiler for input files
  202.                    whose names end in this suffix.  */
  203.   char *spec;            /* To use this compiler, pass this spec
  204.                    to do_spec.  */
  205. };
  206.  
  207. /* Here are the specs for compiling files with various known suffixes.
  208.    A file that does not end in any of these suffixes will be passed
  209.    unchanged to the loader and nothing else will be done to it.  */
  210.  
  211. struct compiler compilers[] =
  212. {
  213.   {".c",
  214.    "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{T} \
  215.         -undef -D__GNU__ -D__GNUC__ %{ansi:-T -$ -D__STRICT_ANSI__} %{!ansi:%p}\
  216.         %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic}\
  217.     %{Wcomment} %{Wtrigraphs} %{Wall} %C\
  218.         %i %{!M*:%{!E:%g.cpp}}%{E:%{o*}}%{M*:%{o*}}\n\
  219.     %{!M*:%{!E:cc1 %g.cpp %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*}\
  220.            %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
  221.            %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
  222.            %{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %g.s}\n\
  223.               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
  224.                       %g.s %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }}}"},
  225.   {".s",
  226.    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
  227.             %i %{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\n }"},
  228.   /* Mark end of table */
  229.   {0, 0}
  230. };
  231.  
  232. /* Here is the spec for running the linker, after compiling all files.  */
  233. #ifdef atarist
  234.  
  235. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  236.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  237.  %{y*} %{!nostdlib:%S} \
  238.  %{L*} %o %{!nostdlib:%s %{g:-lg} %L}\n }}}}";
  239.  
  240. #else
  241.  
  242. char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
  243.  %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  244.  %{y*} %{!nostdlib:%S} \
  245.  %{L*} %o %{!nostdlib:gnulib%s %{g:-lg} %L}\n }}}}";
  246.  
  247. #endif
  248.  
  249.  
  250. /* Record the names of temporary files we tell compilers to write,
  251.    and delete them at the end of the run.  */
  252.  
  253. /* This is the common prefix we use to make temp file names.
  254.    It is chosen once for each run of this program.
  255.    It is substituted into a spec by %g.
  256.    Thus, all temp file names contain this prefix.
  257.    In practice, all temp file names start with this prefix.
  258.    The prefix starts with `/tmp'.  */
  259.  
  260. char *temp_filename;
  261.  
  262. /* Length of the prefix.  */
  263.  
  264. int temp_filename_length;
  265.  
  266. /* Define the list of temporary files to delete.  */
  267.  
  268. struct temp_file
  269. {
  270.   char *name;
  271.   struct temp_file *next;
  272.   int success_only;        /* Nonzero means delete this file
  273.                    only if compilation succeeds fully.  */
  274. };
  275.  
  276. struct temp_file *temp_file_queue;
  277.  
  278. /* Record FILENAME as a file to be deleted automatically.
  279.    SUCCESS_ONLY nonzero means delete it only if all compilation succeeds;
  280.    otherwise delete it in any case.  */
  281.  
  282. void
  283. record_temp_file (filename, success_only)
  284.      char *filename;
  285.      int success_only;
  286. {
  287.   register struct temp_file *temp;
  288.   register char *name;
  289.   temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  290.   name = (char *) xmalloc (strlen (filename) + 1);
  291.   strcpy (name, filename);
  292.   temp->next = temp_file_queue;
  293.   temp->name = name;
  294.   temp->success_only = success_only;
  295.   temp_file_queue = temp;
  296. }
  297.  
  298. /* Delete all the temporary files whose names we previously recorded.
  299.    SUCCESS nonzero means "delete on success only" files should be deleted.  */
  300.  
  301. void
  302. delete_temp_files (success)
  303.      int success;
  304. {
  305.   register struct temp_file *temp;
  306.   for (temp = temp_file_queue; temp; temp = temp->next)
  307.     if (success || ! temp->success_only)
  308.       {
  309. #ifdef DEBUG
  310.     int i;
  311.     printf ("Delete %s? (y or n) ", temp->name);
  312.     fflush (stdout);
  313.     i = getchar ();
  314.     if (i != '\n')
  315.       while (getchar () != '\n') ;
  316.     if (i == 'y' || i == 'Y')
  317. #endif /* DEBUG */
  318.       unlink (temp->name);
  319.       }
  320.   temp_file_queue = 0;
  321. }
  322.  
  323. /* Compute a string to use as the base of all temporary file names.
  324.    It is substituted for %g.  */
  325.  
  326. void
  327. choose_temp_base ()
  328. {
  329. #ifndef atarist
  330.   register char *foo = "/tmp/ccXXXXXX";
  331.   temp_filename = (char *) xmalloc (strlen (foo) + 1);
  332.   strcpy (temp_filename, foo);
  333. #else
  334.   char * env_temp = (char *)getenv("TEMP");
  335.  
  336.   if (!env_temp)
  337.     env_temp = "\\temp";
  338.   temp_filename = concat(env_temp, "\\", "gcc-temp");
  339. #endif
  340.  
  341. #ifndef atarist
  342.   mktemp (temp_filename);
  343. #endif
  344.   temp_filename_length = strlen (temp_filename);
  345. }
  346.  
  347. /* Accumulate a command (program name and args), and run it.  */
  348.  
  349. /* Vector of pointers to arguments in the current line of specifications.  */
  350.  
  351. char **argbuf;
  352.  
  353. /* Number of elements allocated in argbuf.  */
  354.  
  355. int argbuf_length;
  356.  
  357. /* Number of elements in argbuf currently in use (containing args).  */
  358.  
  359. int argbuf_index;
  360.  
  361. /* Flag indicating whether we should print the command and arguments */
  362.  
  363. unsigned char vflag;
  364.  
  365. /* User-specified prefix to attach to command names,
  366.    or 0 if none specified.  */
  367.  
  368. char *user_exec_prefix = 0;
  369.  
  370. /* Default prefixes to attach to command names.  */
  371.  
  372. #ifndef STANDARD_EXEC_PREFIX
  373. #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
  374. #endif /* !defined STANDARD_EXEC_PREFIX */
  375.  
  376. char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
  377. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  378.  
  379. char *standard_startfile_prefix = "/lib/";
  380. char *standard_startfile_prefix_1 = "/usr/lib/";
  381.  
  382. /* Clear out the vector of arguments (after a command is executed).  */
  383.  
  384. void
  385. clear_args ()
  386. {
  387.   argbuf_index = 0;
  388. }
  389.  
  390. /* Add one argument to the vector at the end.
  391.    This is done when a space is seen or at the end of the line.
  392.    If TEMPNAMEP is nonzero, this arg is a file that should be deleted
  393.    at the end of compilation.  (If TEMPNAMEP is 2, delete the file
  394.    only if compilation is fully successful.)  */
  395.  
  396. void
  397. store_arg (arg, tempnamep)
  398.      char *arg;
  399.      int tempnamep;
  400. {
  401.   if (argbuf_index + 1 == argbuf_length)
  402.     {
  403.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  404.     }
  405.  
  406.   argbuf[argbuf_index++] = arg;
  407.   argbuf[argbuf_index] = 0;
  408.  
  409.   if (tempnamep)
  410.     record_temp_file (arg, tempnamep == 2);
  411. }
  412.  
  413. /* Execute the command specified by the arguments on the current line of spec.
  414.    Returns 0 if successful, -1 if failed.  */
  415.  
  416. int
  417. execute ()
  418. {
  419.   int pid;
  420.   int status;
  421.   int size;
  422.   char *temp;
  423.   int win = 0;
  424.  
  425.   size = strlen (standard_exec_prefix);
  426.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  427.     size = strlen (user_exec_prefix);
  428.   if (strlen (standard_exec_prefix_1) > size)
  429.     size = strlen (standard_exec_prefix_1);
  430.   size += strlen (argbuf[0]) + 1;
  431. #ifdef atarist
  432.   size += 5;        /* slush for ".ttp" */
  433. #endif
  434.   temp = (char *) alloca (size);
  435.  
  436.   /* Determine the filename to execute.  */
  437.  
  438.   if (user_exec_prefix)
  439.     {
  440.       strcpy (temp, user_exec_prefix);
  441.       strcat (temp, argbuf[0]);
  442. #ifdef atarist
  443.     strcat (temp, ".ttp");
  444. #endif
  445.       win = (access (temp, X_OK) == 0);
  446.     }
  447.  
  448.   if (!win)
  449.     {
  450.       strcpy (temp, standard_exec_prefix);
  451.       strcat (temp, argbuf[0]);
  452. #ifdef atarist
  453.     strcat (temp, ".ttp");
  454. #endif
  455.       win = (access (temp, X_OK) == 0);
  456.     }
  457.  
  458.   if (!win)
  459.     {
  460.       strcpy (temp, standard_exec_prefix_1);
  461.       strcat (temp, argbuf[0]);
  462. #ifdef atarist
  463.     strcat (temp, ".ttp");
  464. #endif
  465.       win = (access (temp, X_OK) == 0);
  466.     }
  467.  
  468.   if (vflag)
  469.     {
  470.       int i;
  471.       for (i = 0; argbuf[i]; i++)
  472.     {
  473.       if (i == 0 && win)
  474.         fprintf (stderr, " %s", temp);
  475.       else
  476.         fprintf (stderr, " %s", argbuf[i]);
  477.     }
  478.       fprintf (stderr, "\n");
  479.       fflush (stderr);
  480. #ifdef DEBUG
  481.       fprintf (stderr, "\nGo ahead? (y or n) ");
  482.       fflush (stderr);
  483.       i = getchar ();
  484.       if (i != '\n')
  485.     while (getchar () != '\n') ;
  486.       if (i != 'y' && i != 'Y')
  487.     return 0;
  488. #endif                /* DEBUG */
  489.     }
  490.  
  491. #ifndef atarist
  492. #ifdef USG
  493.   pid = fork ();
  494.   if (pid < 0)
  495.     pfatal_with_name ("fork");
  496. #else
  497.   pid = vfork ();
  498.   if (pid < 0)
  499.     pfatal_with_name ("vfork");
  500. #endif
  501.   if (pid == 0)
  502.     {
  503.       if (win)
  504.     execv (temp, argbuf);
  505.       else
  506.     execvp (argbuf[0], argbuf);
  507.       perror_with_name (argbuf[0]);
  508.       _exit (65);
  509.     }
  510.   wait (&status);
  511.   if ((status & 0x7F) != 0)
  512.     fatal ("Program %s got fatal signal %d.", argbuf[0], (status & 0x7F));
  513.   if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  514.     return -1;
  515. #else
  516.  
  517.     {
  518.     char * sizep = (char * ) alloca(256);
  519.     char * cmdp = sizep + 1;
  520.     register int iii;
  521.  
  522.     *cmdp = '\0';
  523.     for (iii = 1 ; (argbuf[iii] != 0) ; iii++ )
  524.         {
  525.         strcat (cmdp, argbuf[iii]);
  526.         strcat (cmdp, " ");
  527.         }
  528.     *sizep = strlen(cmdp);
  529.     if (!win)
  530.         temp = argbuf[0];
  531. /*    printf("exec '%s' '%s'\n", temp, cmdp); */
  532.     iii = Pexec(0, temp, sizep, 0);
  533. /*    printf(" ... -> %d\n", iii); */
  534.     return(iii);
  535.     }
  536. #endif
  537.   return 0;
  538. }
  539.  
  540. /* Find all the switches given to us
  541.    and make a vector describing them.
  542.    The elements of the vector a strings, one per switch given.
  543.    If a switch uses the following argument, then the `part1' field
  544.    is the switch itself and the `part2' field is the following argument.  */
  545.  
  546. struct switchstr
  547. {
  548.   char *part1;
  549.   char *part2;
  550. };
  551.  
  552. struct switchstr *switches;
  553.  
  554. int n_switches;
  555.  
  556. /* Also a vector of input files specified.  */
  557.  
  558. char **infiles;
  559.  
  560. int n_infiles;
  561.  
  562. /* And a vector of corresponding output files is made up later.  */
  563.  
  564. char **outfiles;
  565.  
  566. char *
  567. make_switch (p1, s1, p2, s2)
  568.      char *p1;
  569.      int s1;
  570.      char *p2;
  571.      int s2;
  572. {
  573.   register char *new;
  574.   if (p2 && s2 == 0)
  575.     s2 = strlen (p2);
  576.   new = (char *) xmalloc (s1 + s2 + 2);
  577.   bcopy (p1, new, s1);
  578.   if (p2)
  579.     {
  580.       new[s1++] = ' ';
  581.       bcopy (p2, new + s1, s2);
  582.     }
  583.   new[s1 + s2] = 0;
  584.   return new;
  585. }
  586.  
  587. /* Create the vector `switches' and its contents.
  588.    Store its length in `n_switches'.  */
  589.  
  590. void
  591. process_command (argc, argv)
  592.      int argc;
  593.      char **argv;
  594. {
  595.   register int i;
  596.   n_switches = 0;
  597.   n_infiles = 0;
  598.  
  599.   /* Scan argv twice.  Here, the first time, just count how many switches
  600.      there will be in their vector, and how many input files in theirs.
  601.      Here we also parse the switches that cc itself uses (e.g. -v).  */
  602.  
  603.   for (i = 1; i < argc; i++)
  604.     {
  605.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  606.     {
  607.       register char *p = &argv[i][1];
  608.       register int c = *p;
  609.  
  610.       switch (c)
  611.         {
  612.         case 'B':
  613.           user_exec_prefix = p + 1;
  614.           break;
  615.  
  616.         case 'v':    /* Print our subcommands and print versions.  */
  617.           vflag++;
  618.           n_switches++;
  619.           break;
  620.  
  621.         default:
  622.           n_switches++;
  623.  
  624.           if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  625.         i++;
  626.         }
  627.     }
  628.       else
  629.     n_infiles++;
  630.     }
  631.  
  632.   /* Then create the space for the vectors and scan again.  */
  633.  
  634.   switches = ((struct switchstr *)
  635.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  636.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  637.   n_switches = 0;
  638.   n_infiles = 0;
  639.  
  640.   /* This, time, copy the text of each switch and store a pointer
  641.      to the copy in the vector of switches.
  642.      Store all the infiles in their vector.  */
  643.  
  644.   for (i = 1; i < argc; i++)
  645.     {
  646.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  647.     {
  648.       register char *p = &argv[i][1];
  649.       register int c = *p;
  650.  
  651.       if (c == 'B')
  652.         continue;
  653.       switches[n_switches].part1 = p;
  654.       if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  655.         switches[n_switches].part2 = argv[++i];
  656.       else
  657.         switches[n_switches].part2 = 0;
  658.       n_switches++;
  659.     }
  660.       else
  661.     infiles[n_infiles++] = argv[i];
  662.     }
  663.  
  664.   switches[n_switches].part1 = 0;
  665.   infiles[n_infiles] = 0;
  666. }
  667.  
  668. /* Process a spec string, accumulating and running commands.  */
  669.  
  670. /* These variables describe the input file name.
  671.    input_file_number is the index on outfiles of this file,
  672.    so that the output file name can be stored for later use by %o.
  673.    input_basename is the start of the part of the input file
  674.    sans all directory names, and basename_length is the number
  675.    of characters starting there excluding the suffix .c or whatever.  */
  676.  
  677. char *input_filename;
  678. int input_file_number;
  679. int input_filename_length;
  680. int basename_length;
  681. char *input_basename;
  682.  
  683. /* These are variables used within do_spec and do_spec_1.  */
  684.  
  685. /* Nonzero if an arg has been started and not yet terminated
  686.    (with space, tab or newline).  */
  687. int arg_going;
  688.  
  689. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  690.    is a temporary file name.  */
  691. int delete_this_arg;
  692.  
  693. /* Nonzero means %w has been seen; the next arg to be terminated
  694.    is the output file name of this compilation.  */
  695. int this_is_output_file;
  696.  
  697. /* Nonzero means %s has been seen; the next arg to be terminated
  698.    is the name of a library file and we should try the standard
  699.    search dirs for it.  */
  700. int this_is_library_file;
  701.  
  702. #ifdef atarist
  703. /* I don't know why this is necessary.  Recursive calls to do_spec_1
  704.    end up ignoring the error code from calls to execute().  That causes
  705.    do_spec to get a 0 return value, and do_spec_1("\n"), which causes the
  706.    command to get executed again.
  707. */
  708. int execute_return_error = 0;
  709. #endif
  710.  
  711. /* Process the spec SPEC and run the commands specified therein.
  712.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  713.  
  714. int
  715. do_spec (spec)
  716.      char *spec;
  717. {
  718.   int value;
  719.  
  720.   clear_args ();
  721.   arg_going = 0;
  722.   delete_this_arg = 0;
  723.   this_is_output_file = 0;
  724.   this_is_library_file = 0;
  725.  
  726.   value = do_spec_1 (spec, 0);
  727. #ifdef atarist
  728.   if (!value && execute_return_error)
  729.     {
  730.     value = execute_return_error;
  731.     execute_return_error = 0;
  732.     }
  733. #endif
  734.   if (value == 0)
  735.     value = do_spec_1 ("\n", 0);
  736.   return value;
  737. }
  738.  
  739. /* Process the sub-spec SPEC as a portion of a larger spec.
  740.    This is like processing a whole spec except that we do
  741.    not initialize at the beginning and we do not supply a
  742.    newline by default at the end.
  743.    INSWITCH nonzero means don't process %-sequences in SPEC;
  744.    in this case, % is treated as an ordinary character.
  745.    This is used while substituting switches.
  746.    INSWITCH nonzero also causes SPC not to terminate an argument.
  747.  
  748.    Value is zero unless a line was finished
  749.    and the command on that line reported an error.  */
  750.  
  751. int
  752. do_spec_1 (spec, inswitch)
  753.      char *spec;
  754.      int inswitch;
  755. {
  756.   register char *p = spec;
  757.   register int c;
  758.   char *string;
  759.  
  760.   while (c = *p++)
  761.     /* If substituting a switch, treat all chars like letters.
  762.        Otherwise, NL, SPC, TAB and % are special.  */
  763.     switch (inswitch ? 'a' : c)
  764.       {
  765. #ifdef atarist
  766. /* this stuff added by jrd.  if see '$', expect name of env var, delimited
  767.    by '$'.  Find it's value, and subst it in. */
  768.  
  769.       case '$':
  770.     {
  771.     char varname[32];        /* should be enough */
  772.     char * value;            /* deciphered value string */
  773.     int i;
  774.  
  775.     for (i = 0 ; ((c = *p++) != '$') ; i++)
  776.         varname[i] = c;
  777.     varname[i] = '\0';
  778.     if (strlen(&varname) > 0)
  779.         {
  780.         value = (char * )getenv(&varname);
  781.         if (value)
  782.             do_spec_1(value, 0);
  783.             else
  784.             do_spec_1(".", 0);    /* a compleat kludge... */
  785.         }
  786.     break;
  787.     }
  788. #endif
  789.       case '\n':
  790.     /* End of line: finish any pending argument,
  791.        then run the pending command if one has been started.  */
  792.     if (arg_going)
  793.       {
  794.         obstack_1grow (&obstack, 0);
  795.         string = obstack_finish (&obstack);
  796.         if (this_is_library_file)
  797.           string = find_file (string);
  798.         store_arg (string, delete_this_arg);
  799.         if (this_is_output_file)
  800.           outfiles[input_file_number] = string;
  801.       }
  802.     arg_going = 0;
  803.     if (argbuf_index)
  804.       {
  805.         int value = execute ();
  806. #ifdef atarist
  807.         if (!execute_return_error)
  808.             execute_return_error = value;
  809. #endif
  810.         if (value)
  811.           return value;
  812.       }
  813.     /* Reinitialize for a new command, and for a new argument.  */
  814.     clear_args ();
  815.     arg_going = 0;
  816.     delete_this_arg = 0;
  817.     this_is_output_file = 0;
  818.     this_is_library_file = 0;
  819.     break;
  820.  
  821.       case '\t':
  822.       case ' ':
  823.     /* Space or tab ends an argument if one is pending.  */
  824.     if (arg_going)
  825.       {
  826.         obstack_1grow (&obstack, 0);
  827.         string = obstack_finish (&obstack);
  828.         if (this_is_library_file)
  829.           string = find_file (string);
  830.         store_arg (string, delete_this_arg);
  831.         if (this_is_output_file)
  832.           outfiles[input_file_number] = string;
  833.       }
  834.     /* Reinitialize for a new argument.  */
  835.     arg_going = 0;
  836.     delete_this_arg = 0;
  837.     this_is_output_file = 0;
  838.     this_is_library_file = 0;
  839.     break;
  840.  
  841.       case '%':
  842.     switch (c = *p++)
  843.       {
  844.       case 0:
  845.         fatal ("Invalid specification!  Bug in cc.");
  846.  
  847.       case 'i':
  848.         obstack_grow (&obstack, input_filename, input_filename_length);
  849.         arg_going = 1;
  850.         break;
  851.  
  852.       case 'b':
  853.         obstack_grow (&obstack, input_basename, basename_length);
  854.         arg_going = 1;
  855.         break;
  856.  
  857.       case 'p':
  858.         do_spec_1 (CPP_PREDEFINES, 0);
  859.         break;
  860.  
  861.       case 'g':
  862.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  863.         delete_this_arg = 1;
  864.         arg_going = 1;
  865.         break;
  866.  
  867.       case 'd':
  868.         delete_this_arg = 2;
  869.         break;
  870.  
  871.       case 'w':
  872.         this_is_output_file = 1;
  873.         break;
  874.  
  875.       case 's':
  876.         this_is_library_file = 1;
  877.         break;
  878.  
  879.       case 'o':
  880.         {
  881.           register int f;
  882.           for (f = 0; f < n_infiles; f++)
  883.         store_arg (outfiles[f], 0);
  884.         }
  885.         break;
  886.  
  887.       case 'a':
  888.         do_spec_1 (ASM_SPEC, 0);
  889.         break;
  890.  
  891.       case 'c':
  892.         do_spec_1 (SIGNED_CHAR_SPEC, 0);
  893.         break;
  894.  
  895.       case 'C':
  896.         do_spec_1 (CPP_SPEC, 0);
  897.         break;
  898.  
  899.       case 'l':
  900.         do_spec_1 (LINK_SPEC, 0);
  901.         break;
  902.  
  903.       case 'L':
  904.         do_spec_1 (LIB_SPEC, 0);
  905.         break;
  906.  
  907.       case 'S':
  908.         do_spec_1 (STARTFILE_SPEC, 0);
  909.         break;
  910.  
  911.       case '{':
  912.         p = handle_braces (p);
  913.         if (p == 0)
  914.           return -1;
  915.         break;
  916.  
  917.       case '%':
  918.         obstack_1grow (&obstack, '%');
  919.         break;
  920.  
  921.       default:
  922. #ifdef atarist
  923.         fprintf(stderr, "Bogus char '%c' found at pos %d of spec '%s'\n",
  924.         c, (p - spec - 1), spec);
  925. #endif
  926.         abort ();
  927.       }
  928.     break;
  929.  
  930.       default:
  931.     /* Ordinary character: put it into the current argument.  */
  932.     obstack_1grow (&obstack, c);
  933.     arg_going = 1;
  934.       }
  935.  
  936.   return 0;        /* End of string */
  937. }
  938.  
  939. /* Return 0 if we call do_spec_1 and that returns -1.  */
  940.  
  941. char *
  942. handle_braces (p)
  943.      register char *p;
  944. {
  945.   register char *q;
  946.   int negate = *p == '!';
  947.   char *filter;
  948.  
  949.   if (negate) ++p;
  950.  
  951.   filter = p;
  952.   while (*p != ':' && *p != '}') p++;
  953.   if (*p != '}')
  954.     {
  955.       register int count = 1;
  956.       q = p + 1;
  957.       while (count > 0)
  958.     {
  959.       if (*q == '{')
  960.         count++;
  961.       else if (*q == '}')
  962.         count--;
  963.       else if (*q == 0)
  964.         abort ();
  965.       q++;
  966.     }
  967.     }
  968.   else
  969.     q = p + 1;
  970.  
  971.   if (p[-1] == '*' && p[0] == '}')
  972.     {
  973.       /* Substitute all matching switches as separate args.  */
  974.       register int i;
  975.       --p;
  976.       for (i = 0; i < n_switches; i++)
  977.     if (!strncmp (switches[i].part1, filter, p - filter))
  978.       {
  979.         give_switch (i);
  980.       }
  981.     }
  982.   else
  983.     {
  984.       /* Test for presence of the specified switch.  */
  985.       register int i;
  986.       int present = 0;
  987.  
  988.       /* If name specified ends in *, as in {x*:...},
  989.      check for presence of any switch name starting with x.  */
  990.       if (p[-1] == '*')
  991.     {
  992.       for (i = 0; i < n_switches; i++)
  993.         {
  994.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  995.         {
  996.           present = 1;
  997.           break;
  998.         }
  999.         }
  1000.     }
  1001.       /* Otherwise, check for presence of exact name specified.  */
  1002.       else
  1003.     {
  1004.       for (i = 0; i < n_switches; i++)
  1005.         {
  1006.           if (!strncmp (switches[i].part1, filter, p - filter)
  1007.           && switches[i].part1[p - filter] == 0)
  1008.         {
  1009.           present = 1;
  1010.           break;
  1011.         }
  1012.         }
  1013.     }
  1014.  
  1015.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  1016.      then substitute either the switch or the specified
  1017.      conditional text.  */
  1018.       if (present != negate)
  1019.     {
  1020.       if (*p == '}')
  1021.         {
  1022.           give_switch (i);
  1023.         }
  1024.       else
  1025.         {
  1026.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  1027.         return 0;
  1028.         }
  1029.     }
  1030.     }
  1031.  
  1032.   return q;
  1033. }
  1034.  
  1035. /* Pass a switch to the current accumulating command
  1036.    in the same form that we received it.
  1037.    SWITCHNUM identifies the switch; it is an index into
  1038.    the vector of switches gcc received, which is `switches'.
  1039.    This cannot fail since it never finishes a command line.  */
  1040.  
  1041. give_switch (switchnum)
  1042.      int switchnum;
  1043. {
  1044.   do_spec_1 ("-", 0);
  1045.   do_spec_1 (switches[switchnum].part1, 1);
  1046.   do_spec_1 (" ", 0);
  1047.   if (switches[switchnum].part2 != 0)
  1048.     {
  1049.       do_spec_1 (switches[switchnum].part2, 1);
  1050.       do_spec_1 (" ", 0);
  1051.     }
  1052. }
  1053.  
  1054. /* Search for a file named NAME trying various prefixes including the
  1055.    user's -B prefix and some standard ones.
  1056.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  1057.  
  1058. char *
  1059. find_file (name)
  1060.      char *name;
  1061. {
  1062.   int size;
  1063.   char *temp;
  1064.   int win = 0;
  1065.  
  1066.   /* Compute maximum size of NAME plus any prefix we will try.  */
  1067.  
  1068.   size = strlen (standard_exec_prefix);
  1069.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  1070.     size = strlen (user_exec_prefix);
  1071.   if (strlen (standard_exec_prefix_1) > size)
  1072.     size = strlen (standard_exec_prefix_1);
  1073.   if (strlen (standard_startfile_prefix) > size)
  1074.     size = strlen (standard_startfile_prefix);
  1075.   if (strlen (standard_startfile_prefix_1) > size)
  1076.     size = strlen (standard_startfile_prefix_1);
  1077.   size += strlen (name) + 1;
  1078.  
  1079.   temp = (char *) alloca (size);
  1080.  
  1081.   if (user_exec_prefix)
  1082.     {
  1083.       strcpy (temp, user_exec_prefix);
  1084.       strcat (temp, name);
  1085.       win = (access (temp, R_OK) == 0);
  1086.     }
  1087.  
  1088.   if (!win)
  1089.     {
  1090.       strcpy (temp, standard_exec_prefix);
  1091.       strcat (temp, name);
  1092.       win = (access (temp, R_OK) == 0);
  1093.     }
  1094.  
  1095.   if (!win)
  1096.     {
  1097.       strcpy (temp, standard_exec_prefix_1);
  1098.       strcat (temp, name);
  1099.       win = (access (temp, R_OK) == 0);
  1100.     }
  1101.  
  1102.   if (!win)
  1103.     {
  1104.       strcpy (temp, standard_startfile_prefix);
  1105.       strcat (temp, name);
  1106.       win = (access (temp, R_OK) == 0);
  1107.     }
  1108.  
  1109.   if (!win)
  1110.     {
  1111.       strcpy (temp, standard_startfile_prefix_1);
  1112.       strcat (temp, name);
  1113.       win = (access (temp, R_OK) == 0);
  1114.     }
  1115.  
  1116.   if (!win)
  1117.     {
  1118. #ifdef atarist
  1119.       strcpy (temp, ".\\");
  1120. #else
  1121.       strcpy (temp, "./");
  1122. #endif
  1123.       strcat (temp, name);
  1124.       win = (access (temp, R_OK) == 0);
  1125.     }
  1126.  
  1127.   if (win)
  1128.     return save_string (temp, strlen (temp));
  1129.   return name;
  1130. }
  1131.  
  1132. /* Name with which this program was invoked.  */
  1133.  
  1134. char *programname;
  1135.  
  1136. /* On fatal signals, delete all the temporary files.  */
  1137.  
  1138. #ifndef atarist
  1139. void
  1140. fatal_error (signum)
  1141.      int signum;
  1142. {
  1143.   signal (signum, SIG_DFL);
  1144.   delete_temp_files (0);
  1145.   /* Get the same signal again, this time not handled,
  1146.      so its normal effect occurs.  */
  1147.   kill (getpid (), signum);
  1148. }
  1149. #endif
  1150.  
  1151. int
  1152. main (argc, argv)
  1153.      int argc;
  1154.      char **argv;
  1155. {
  1156.   register int i;
  1157.   int value;
  1158.   int nolink = 0;
  1159.   int error = 0;
  1160.  
  1161. #ifdef atarist
  1162.   programname = "gcc";
  1163. #else
  1164.   programname = argv[0];
  1165. #endif
  1166.  
  1167. #ifndef atarist
  1168.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1169.     signal (SIGINT, fatal_error);
  1170.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1171.     signal (SIGHUP, fatal_error);
  1172.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1173.     signal (SIGTERM, fatal_error);
  1174. #endif
  1175.  
  1176.   argbuf_length = 10;
  1177.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1178.  
  1179.   obstack_init (&obstack);
  1180.  
  1181.   choose_temp_base ();
  1182.  
  1183.   /* Make a table of what switches there are (switches, n_switches).
  1184.      Make a table of specified input files (infiles, n_infiles).  */
  1185.  
  1186.   process_command (argc, argv);
  1187.  
  1188. #ifdef atarist
  1189. /* if no default dir specified for executables, look for an env var
  1190.    called 'GCCEXEC' and use that */
  1191.  
  1192.   if (!user_exec_prefix)
  1193.       {
  1194.     user_exec_prefix = getenv("GCCEXEC");
  1195.     }
  1196. #endif /* atarist */
  1197.  
  1198.   if (vflag)
  1199.     {
  1200.       extern char *version_string;
  1201.       printf ("gcc version %s\n", version_string);
  1202.       if (n_infiles == 0)
  1203.     exit (0);
  1204.     }
  1205.  
  1206.   if (n_infiles == 0)
  1207.     fatal ("No source or object files specified.");
  1208.  
  1209.   /* Make a place to record the compiler output file names
  1210.      that correspond to the input files.  */
  1211.  
  1212.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1213.   bzero (outfiles, n_infiles * sizeof (char *));
  1214.  
  1215.   for (i = 0; i < n_infiles; i++)
  1216.     {
  1217.       /* First figure out which compiler from the file's suffix.  */
  1218.       
  1219.       register struct compiler *cp;
  1220.  
  1221.       /* Tell do_spec what to substitute for %i.  */
  1222.  
  1223.       input_filename = infiles[i];
  1224.       input_filename_length = strlen (input_filename);
  1225.       input_file_number = i;
  1226.  
  1227.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1228.  
  1229.       outfiles[i] = input_filename;
  1230.  
  1231.       for (cp = compilers; cp->spec; cp++)
  1232.     {
  1233.       if (strlen (cp->suffix) < input_filename_length
  1234.           && !strcmp (cp->suffix,
  1235.               infiles[i] + input_filename_length
  1236.               - strlen (cp->suffix)))
  1237.         {
  1238.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1239.           /* First say how much of input_filename to substitute for %b  */
  1240.           register char *p;
  1241.  
  1242.           input_basename = input_filename;
  1243.           for (p = input_filename; *p; p++)
  1244. #ifdef atarist
  1245.         if (*p == '\\')
  1246. #else
  1247.         if (*p == '/')
  1248. #endif
  1249.           input_basename = p + 1;
  1250.           basename_length = (input_filename_length - strlen (cp->suffix)
  1251.                  - (input_basename - input_filename));
  1252.           value = do_spec (cp->spec);
  1253.           if (value < 0)
  1254.         error = 1;
  1255. #ifdef atarist
  1256. /* is this necessary? */
  1257.            else
  1258.           if (value == FATAL_EXIT_CODE)
  1259.         error = 1;
  1260. #endif
  1261.           break;
  1262.         }
  1263.     }
  1264.  
  1265.       /* If this file's name does not contain a recognized suffix,
  1266.      don't do anything to it, but do feed it to the link spec
  1267.      since its name is in outfiles.  */
  1268.     }
  1269.  
  1270.   /* Run ld to link all the compiler output files.  */
  1271.  
  1272.   if (! nolink && error == 0)
  1273.     {
  1274.       value = do_spec (link_spec);
  1275.       if (value < 0)
  1276.     error = 1;
  1277.     }
  1278.  
  1279.   /* Delete some or all of the temporary files we made.  */
  1280.  
  1281.   delete_temp_files (error == 0);
  1282.  
  1283.   exit (error);
  1284. }
  1285.  
  1286. xmalloc (size)
  1287.      int size;
  1288. {
  1289.   register int value = malloc (size);
  1290.   if (value == 0)
  1291.     fatal ("Virtual memory full.");
  1292.   return value;
  1293. }
  1294.  
  1295. xrealloc (ptr, size)
  1296.      int ptr, size;
  1297. {
  1298.   register int value = realloc (ptr, size);
  1299.   if (value == 0)
  1300.     fatal ("Virtual memory full.");
  1301.   return value;
  1302. }
  1303.  
  1304. fatal (msg, arg1, arg2)
  1305.      char *msg, *arg1, *arg2;
  1306. {
  1307.   error (msg, arg1, arg2);
  1308.   delete_temp_files (0);
  1309.   exit (1);
  1310. }
  1311.  
  1312. error (msg, arg1, arg2)
  1313.      char *msg, *arg1, *arg2;
  1314. {
  1315.   fprintf (stderr, "%s: ", programname);
  1316.   fprintf (stderr, msg, arg1, arg2);
  1317.   fprintf (stderr, "\n");
  1318. }
  1319.  
  1320. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1321.  
  1322. char *
  1323. concat (s1, s2, s3)
  1324.      char *s1, *s2, *s3;
  1325. {
  1326.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1327.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1328.  
  1329.   strcpy (result, s1);
  1330.   strcpy (result + len1, s2);
  1331.   strcpy (result + len1 + len2, s3);
  1332.   *(result + len1 + len2 + len3) = 0;
  1333.  
  1334.   return result;
  1335. }
  1336.  
  1337. char *
  1338. save_string (s, len)
  1339.      char *s;
  1340.      int len;
  1341. {
  1342.   register char *result = (char *) xmalloc (len + 1);
  1343.  
  1344.   bcopy (s, result, len);
  1345.   result[len] = 0;
  1346.   return result;
  1347. }
  1348.  
  1349. pfatal_with_name (name)
  1350.      char *name;
  1351. {
  1352.   extern int errno, sys_nerr;
  1353.   extern char *sys_errlist[];
  1354.   char *s;
  1355.  
  1356. #ifdef atarist
  1357.   if ((errno > sys_nerr) && (errno < 0))
  1358. #else
  1359.   if (errno < sys_nerr)
  1360. #endif
  1361.     s = concat ("", sys_errlist[errno], " for %s");
  1362.   else
  1363.     s = "cannot open %s";
  1364.   fatal (s, name);
  1365. }
  1366.  
  1367. perror_with_name (name)
  1368.      char *name;
  1369. {
  1370.   extern int errno, sys_nerr;
  1371.   extern char *sys_errlist[];
  1372.   char *s;
  1373.  
  1374. #ifdef atarist
  1375.   if ((errno > sys_nerr) && (errno < 0))
  1376. #else
  1377.   if (errno < sys_nerr)
  1378. #endif
  1379.     s = concat ("", sys_errlist[errno], " for %s");
  1380.   else
  1381.     s = "cannot open %s";
  1382.   error (s, name);
  1383. }
  1384.